package org.fing.edu.uy.admin.mbeanServices;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.management.MBeanServer;
import javax.management.ObjectName;

import org.fing.edu.uy.esbadp.core.interfaces.IMotorMonitorServicesMBean;
import org.jboss.mx.util.MBeanServerLocator;

public class RegisterMBean {

	private static java.util.logging.Logger logger = Logger.getLogger(IMotorMonitorServicesMBean.class.getSimpleName());	
	
	private static RegisterMBean instance;
	private static MBeanServer mbeanJBossServer;
	
	private RegisterMBean(){
		mbeanJBossServer = MBeanServerLocator.locateJBoss();
	}
	
	public static RegisterMBean getInstance(){
		if(instance==null)
			instance = new RegisterMBean();
		return instance;
	}
	
	public void registerMBeanService(Object mBean , String mBeanServiceName){
		try{		
			ObjectName mServiceBeanName = new ObjectName(MBeanServicesConstants.serviceAgentName + mBeanServiceName);
			mbeanJBossServer.registerMBean(mBean, mServiceBeanName);
		}catch(Exception ex ){
			logger.log(Level.SEVERE, "Exception in register JMX Service.", ex);
		}			 
	}
	
	public void registerIMotorMonitorServicesMBean(){
		try{		
			ObjectName mServiceBeanName = new ObjectName(MBeanServicesConstants.serviceAgentName + MBeanServicesConstants.adminMBeanService);
			WsReportServicesMBean mBean = new WsReportServices(WsReportServicesMBean.class);
			mbeanJBossServer.registerMBean(mBean, mServiceBeanName);
		}catch(Exception ex ){
			logger.log(Level.SEVERE, "Exception in register JMX EsbAdaptativeEngineMonitorServices.", ex);
		}			 
	}
	
	public void unRegisterIMotorMonitorServicesMBean(){
		try{
			ObjectName mServiceBeanName = new ObjectName(MBeanServicesConstants.serviceAgentName + MBeanServicesConstants.adminMBeanService);
			mbeanJBossServer.unregisterMBean(mServiceBeanName);
		}catch(Exception ex ){
			logger.log(Level.SEVERE, "Exception in unRegisterAll JMX Service.", ex);
		}			 
	}
	
}
